import React, {Component} from 'react';
import './index.less';
import {base, toast} from 'nc-lightapp-front';
import getSubTableData from "./functions/getSubTableData";
import addLine from "./functions/addLine";
import editLine from './functions/editLine';
import deleteLine from './functions/deleteLine';
import changeLine from './functions/changeLine';
import checkSaveLine from "./functions/checkSaveLine";
import saveLine from "./functions/saveLine";
import beforeTableEdit from "./functions/beforeTableEdit";
import afterTableEdit from "./functions/afterTableEdit";
import updateSubButtonStatus from "./functions/updateSubButtonStatus";
import AlertModal from "../AlertModal";
import addAction from "./functions/addAction";
import hideAction from "./functions/hideAction";
import showAction from "./functions/showAction";
import {debounce} from "../../../../../../hrpub/common/utils/utils";

class SubTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            areaCode: 'hi_psndoc_part',
            tableHeight: ''
        };
        this.onBtnOperation = this.onBtnOperation.bind(this);
        this.getSubTableData = this.getSubTableData.bind(this);
        this.saveLine = this.saveLine.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
        this.afterEventHandle = this.afterEventHandle.bind(this);
        this.cancelEdit = this.cancelEdit.bind(this);
        this.setTableHeight = this.setTableHeight.bind(this);
    }

    componentDidMount() {
        updateSubButtonStatus.call(this, false);
        this.setTableHeight();
        window.addEventListener("resize", debounce(this.setTableHeight));
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.selectedPsn !== this.props.selectedPsn || nextProps.isIncHisPartJob !== this.props.isIncHisPartJob) {
            this.getSubTableData(nextProps.selectedPsn, nextProps.isIncHisPartJob)
        }
    }

    addAction() {
        addAction.call(this);
    }

    hideAction() {
        hideAction.call(this);
    }

    showAction() {
        showAction.call(this);
    }

    getSubTableData(key, isIncHisPartJob) {
        getSubTableData.call(this, key, isIncHisPartJob)
    }

    onBtnOperation(props, btncode, areacode, opt) {
        // 获取选中的行
        let tableData = props.editTable.getClickRowIndex('hi_psndoc_part')
        console.log(tableData)
        //1:新增行;2:插入行;3:修改行;4:兼职变更
        switch (btncode) {
            case 'addline':
                this.editType = 1;
                addLine.call(this);
                break;
            case 'save':
                checkSaveLine.call(this);
                break;
            case 'cancel':
                this.cancelEdit();
                break;
            /*case 'delete':
                if (!tableData || Object.keys(tableData) === 0) {
                    toast({
                        color: 'warning',
                        content: '请选择需要操作的数据！'
                    })
                    return; 
                }
                deleteLine.call(this, tableData.record, tableData.index);
                break;
            case 'edit': 
                if (!tableData) {
                    toast({
                        color: 'warning',
                        content: '请选择需要操作的数据！'
                    })
                    return; 
                }
                editLine.call(this, tableData.record, tableData.index);
                break;
            case 'change': 
                if (!tableData) {
                    toast({
                        color: 'warning',
                        content: '请选择需要操作的数据！'
                    })
                    return; 
                }
                changeLine.call(this, tableData.record, tableData.index);
                break;*/
            default:
                break;
        }
    }

    cancelEdit() {
        const {editTable, disabledAllArea} = this.props;
        editTable.cancelEdit(this.state.areaCode, () => {
            disabledAllArea(false);
            this.showAction();
            updateSubButtonStatus.call(this, false);
            this.props.button.setButtonDisabled({
                'delete': false,
                'edit': false,
                'change': false
            })
        })
    }

    saveLine(isSynWork) {
        const {modal} = this.props;
        modal.close('isSynWork');
        saveLine.call(this, isSynWork);
    }

    beforeEventHandle(props, moduleId, item, index, value, record) {
        this.oladValue = value.value;
        return beforeTableEdit.call(this, moduleId, item.attrcode, record);
    }

    afterEventHandle(props, moduleId, key, value, changedrows, index, record) {
        afterTableEdit.call(this, moduleId, key, record, this.oladValue, index)
    }

    setTableHeight() {
        const mainTable = document.getElementsByClassName('sub-table-tabs');
        if (mainTable[0]) {
            this.setState({
                tableHeight: mainTable[0].clientHeight - 76
            })
        }
    }

    render() {
        const {button, editTable, modal} = this.props;
        const {createModal} = modal;
        return (
            <div className='sub-table-tabs'>
                <div className='sub-table'>
                    {
                        editTable.createEditTable('hi_psndoc_part', {
                            height: this.state.tableHeight,
                            onBeforeEvent: this.beforeEventHandle,
                            onAfterEvent: this.afterEventHandle
                        })
                    }
                </div>
                <div className='sub-table-btns'>
                    {button.createButtonApp({
                        area: 'body',
                        onButtonClick: (props, btncode) => {
                            this.onBtnOperation(props, btncode, 'body', this);
                        }
                    })}
                </div>
                <AlertModal ref={node => this.alertModalNode = node} json={this.props.json}/>
                {
                    createModal('isSynWork', {
                        title: this.props.json['hi6007-000116'], /* 国际化处理： 确认同步*/
                        content: this.props.json['hi6007-000115'], /* 国际化处理： 是否同步履历*/
                        beSureBtnClick: () => this.saveLine(true),
                        size: 'md',
                        backdrop: 'static',
                        rightBtnName: this.props.json['hi6007-000176'], //左侧按钮名称,默认关闭/* 国际化处理： 否,否*/
                        leftBtnName: this.props.json['hi6007-000175'], //右侧按钮名称， 默认确认/* 国际化处理： 是,是*/
                        userControl: true,
                        cancelBtnClick: () => this.saveLine(false)
                    })
                }
            </div>
        );
    }
}

export default SubTable;
